package com.minsus.secretmailbox;

//import android.app.Activity;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.cordova.DroidGap;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class Start extends DroidGap {
	private boolean mFlagExit = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (!isPrefsRegistrationId()) {
			registrationC2DM();
		}

		try {
			super.loadUrl("http://minsus.dothome.co.kr/app/smb/index_a.html");
//			 super.loadUrl("file:///android_asset/www/index_a.html");
			this.appView.addJavascriptInterface(new JavaScriptInterface(),
					"EXTERNALLINK");
		} catch (Exception lException) {
			throw new RuntimeException("hello hello", lException);
		}
	}

	private boolean isPrefsRegistrationId() {
		String id = this
				.getSharedPreferences(this.getString(R.string.prefs_name),
						Context.MODE_PRIVATE)
				.getString(this.getString(R.string.prefs_registration_id), null);

		if (id == null) {
			return false;
		} else {
			return true;
		}
	}

	class JavaScriptInterface {
		public void loadExternalLink(String lUrl) {
			try {
				loadUrl(lUrl);
			} catch (Exception lEx) {
				int i = 0;
			}
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		int action = event.getAction();
		int keyCode = event.getKeyCode();
		if (action == KeyEvent.ACTION_DOWN) {
			switch (keyCode) {
			case KeyEvent.KEYCODE_BACK: {
				if (mFlagExit) {
					this.finish();
				} else {
					mFlagExit = true;
					Toast.makeText(this, "종료 하시려면 뒤로 가기 버튼을 한번더 눌러주세요.",
							Toast.LENGTH_SHORT).show();
					TimerTask timerTask = new TimerTask() {
						@Override
						public void run() {
							mFlagExit = false;
						}
					};
					Timer timer = new Timer();
					timer.schedule(timerTask, 2500);
				}
				return false;
			}
			}
		}

		return super.dispatchKeyEvent(event);
	}

	/**
	 * Registration C2DM
	 */
	private void registrationC2DM() {
		Intent registrationIntent = new Intent(
				"com.google.android.c2dm.intent.REGISTER");
		registrationIntent.putExtra("app",
				PendingIntent.getBroadcast(this, 0, new Intent(), 0));
		// Developer e-mail Address
		registrationIntent.putExtra("sender", "vrothers@gmail.com");
		startService(registrationIntent);
	}

	/**
	 * Unregistration C2DM
	 */
	private void unregistrationC2DM() {
		Intent unregIntent = new Intent(
				"com.google.android.c2dm.intent.UNREGISTER");
		unregIntent.putExtra("app",
				PendingIntent.getBroadcast(this, 0, new Intent(), 0));
		startService(unregIntent);
	}

	// private class AsyncTaskSetRegistrationId extends
	// AsyncTask<String, Integer, String> {
	//
	// private HttpClientPost mHttpClientPost;
	// private boolean mIsRunning = false;
	// private int mTotalCount = 10;
	// private int mCount = 0;
	//
	// public AsyncTaskSetRegistrationId() {
	// mHttpClientPost = new HttpClientPost();
	// mCount = 0;
	// mIsRunning = true;
	// }
	//
	// /**
	// * @param url
	// * @param uid
	// * @param rid
	// * @param name
	// * @param model
	// * @param version
	// * @return String Type
	// */
	// @Override
	// protected String doInBackground(String... params) {
	// String result = null;
	// while (mIsRunning) {
	// try {
	// result = mHttpClientPost.httpSetRegstrationId(params[0],
	// params[1], params[2], params[3], params[4]);
	// } catch (ClientProtocolException e) {
	// e.printStackTrace();
	// } catch (IOException e) {
	// e.printStackTrace();
	// }
	// Log.d("test", "test doInBackground result=" + result);
	// if (result != null || mCount > mTotalCount) {
	// if(result.endsWith("success")) {
	// return "success";
	// } else {
	// return "fail";
	// }
	// }
	// mCount++;
	// }
	// return null;
	// }
	//
	// @Override
	// protected void onPostExecute(String result) {
	// super.onPostExecute(result);
	// mIsRunning = false;
	// mCount = 0;
	// }
	// } // end of class AsyncTaskSetRegistrationId
} // end of class Start
